package com.bootdo.wx.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.wx.domain.DeviceInfoDO;
import com.bootdo.wx.service.DeviceInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备信息
 *
 * Created by Administrator.
 *
 * @autor L - 2020/5/15 - 17:08.
 */

@Controller
@RequestMapping("/wx/deviceInfo")
public class DeviceInfoController {

    @Autowired
    private DeviceInfoService deviceInfoService;

    @GetMapping()
    @RequiresPermissions("wx:deviceInfo:deviceInfo")
    String DeviceInfo() {
        return "wx/deviceInfo/deviceInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("wx:deviceInfo:deviceInfo")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<DeviceInfoDO> deviceInfoList = deviceInfoService.list(query);
        int total = deviceInfoService.count(query);
        PageUtils pageUtils = new PageUtils(deviceInfoList, total);
        return pageUtils;
    }

    /*去检测参数页面*/
    @GetMapping("/testData/{device}")
    @RequiresPermissions("wx:deviceInfo:deviceInfo")
    String testData(@PathVariable("device") String device, Model model) {
        model.addAttribute("id", device);
        return "wx/deviceInfo/testData";
    }

    /**
     * 获取检测参数详情
     */
    @ResponseBody
    @RequestMapping("/getTestData")
    @RequiresPermissions("wx:deviceInfo:deviceInfo")
    public PageUtils getTestData(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<Map<String, Object>> testDataList = deviceInfoService.getTestData(query);
        int total = deviceInfoService.getTestDataCount(query);
        PageUtils pageUtils = new PageUtils(testDataList, total);
        return pageUtils;
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("wx:deviceInfo:add")
    public R save(DeviceInfoDO deviceInfo) {
        if (deviceInfoService.save(deviceInfo) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("wx:deviceInfo:edit")
    public R update(DeviceInfoDO deviceInfo) {
        deviceInfoService.update(deviceInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("wx:deviceInfo:remove")
    public R remove(String id) {
        if (deviceInfoService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("wx:deviceInfo:batchRemove")
    public R batchRemove(@RequestParam("ids[]") String[] ids) {
        deviceInfoService.batchRemove(ids);
        return R.ok();
    }

}
